using Acme.BookStore.Application;
using Acme.BookStore.SqlsugarCore;
using Microsoft.OpenApi.Models;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Auditing;
using Volo.Abp.Autofac;
using Volo.Abp.Swashbuckle;
using Yi.Framework.AspNetCore;
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder;
using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection;

namespace Acme.BookStore.Web;

[DependsOn(
    typeof(AcmeBookStoreSqlSugarCoreModule),
    typeof(AcmeBookStoreApplicationModule),
    typeof(AbpAspNetCoreMvcModule),
    typeof(AbpAutofacModule),
    typeof(AbpSwashbuckleModule),
    typeof(AbpAspNetCoreSerilogModule),
    typeof(AbpAuditingModule),
    typeof(YiFrameworkAspNetCoreModule)
)]
public class AcmeBookStoreWebMoudle : AbpModule
{
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        //动态Api-改进在pre中配置，启动更快
        PreConfigure<AbpAspNetCoreMvcOptions>(options =>
        {
            options.ConventionalControllers.Create(typeof(AcmeBookStoreApplicationModule).Assembly,
                options => options.RemoteServiceName = "default");

            //设定分组
            // options.ConventionalControllers.Create(typeof(YiFrameworkRbacApplicationModule).Assembly,
            //     options => options.RemoteServiceName = "rbac");
            //统一前缀
            options.ConventionalControllers.ConventionalControllerSettings.ForEach(x => x.RootPath = "api/app");
        });
    }

    public override Task ConfigureServicesAsync(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();
        var host = context.Services.GetHostingEnvironment();
        var service = context.Services;
        
        //请求日志
        Configure<AbpAuditingOptions>(options =>
        {
            //默认关闭，开启会有大量的审计日志
            options.IsEnabled = false;
        });
        
        //配置错误处理显示详情
        Configure<AbpExceptionHandlingOptions>(options => { options.SendExceptionsDetailsToClients = true; });
        
        //Swagger
        context.Services.AddYiSwaggerGen<AcmeBookStoreWebMoudle>(options =>
        {
            options.SwaggerDoc("default",
                new OpenApiInfo { Title = "Acme.BookStore.Abp", Version = "v1", Description = "测试" });
        });
        
        //跨域
        
        //速率限制
        //每60秒限制100个请求，滑块添加，分6段
        
        //jwt鉴权
        
        //授权
        return Task.CompletedTask;
    }


    public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        var service = context.ServiceProvider;
        var env = context.GetEnvironment();
        var app = context.GetApplicationBuilder();
        
        app.UseRouting();
        
        if (!env.IsDevelopment())
        {
            //速率限制
            //app.UseRateLimiter();
        }
        
        //swagger
        app.UseYiSwagger();
        
        //静态资源
        app.UseStaticFiles();
        //工作单元
        app.UseUnitOfWork();
       
        //日志记录
        app.UseAbpSerilogEnrichers();
        
        //终节点
        app.UseConfiguredEndpoints();
    }

}